<template>
  <el-dialog
    :title="!dataForm.id ? '新增' : '修改'"
    :close-on-click-modal="false"
    :visible.sync="visible">
    <el-form :model="dataForm" :rules="dataRule" ref="dataForm" @keyup.enter.native="dataFormSubmit()" label-width="80px">
    <el-form-item label="用户名" prop="name">
      <el-input v-model="dataForm.name" placeholder="用户名"></el-input>
    </el-form-item>
    <el-form-item label="电话号码" prop="phone">
      <el-input v-model="dataForm.phone" placeholder="电话号码"></el-input>
    </el-form-item>
    <el-form-item label="短号" prop="dphone">
      <el-input v-model="dataForm.dphone" placeholder="短号"></el-input>
    </el-form-item>
    <el-form-item label="头像连接地址" prop="avatarurl">
 <!--      <el-input v-model="dataForm.avatarurl" placeholder="头像连接地址"></el-input> -->
      <single-upload v-model="dataForm.avatarurl"></single-upload>
    </el-form-item>
    <el-form-item label="余额" prop="showFee">
      <el-input v-model="dataForm.showFee" placeholder="余额"></el-input>
    </el-form-item>
    <el-form-item label="支付宝账户" prop="zfb">
      <el-input v-model="dataForm.zfb" placeholder="支付宝账户"></el-input>
    </el-form-item>
    </el-form>
    <span slot="footer" class="dialog-footer">
      <el-button @click="visible = false">取消</el-button>
      <el-button type="primary" @click="dataFormSubmit()">确定</el-button>
    </span>
  </el-dialog>
</template>

<script>
import singleUpload from "@/components/upload/singleUpload";
export default {
    components: { singleUpload } /* 注册进来 */,
    data () {
      return {
        visible: false,
        dataForm: {
          id: 0,
          name: '',
          phone: '',
          dphone: '',
          version: '',
          avatarurl: '',
          showFee: '',
          zfb: ''
        },
        dataRule: {
          name: [
            { required: true, message: '用户名不能为空', trigger: 'blur' }
          ],
          phone: [
            { required: true, message: '电话号码不能为空', trigger: 'blur' }
          ],
          dphone: [
            { required: true, message: '短号不能为空', trigger: 'blur' }
          ],
          avatarurl: [
            { required: true, message: '头像连接地址不能为空', trigger: 'blur' }
          ],
          showFee: [
            { required: true, message: '余额不能为空', trigger: 'blur' }
          ]
        }
      }
    },
    methods: {
      init (id) {
        this.dataForm.id = id || 0
        this.visible = true
        this.$nextTick(() => {
          this.$refs['dataForm'].resetFields()
          if (this.dataForm.id) {
            this.$http({
              url: this.$http.adornUrl(`/help/user/info/${this.dataForm.id}`),
              method: 'get',
              params: this.$http.adornParams()
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.dataForm.name = data.user.name
                this.dataForm.phone = data.user.phone
                this.dataForm.dphone = data.user.dphone
                this.dataForm.avatarurl = data.user.avatarurl
                this.dataForm.showFee = data.user.showFee
                this.dataForm.zfb = data.user.zfb
              }
            })
          }
        })
      },
      // 表单提交
      dataFormSubmit () {
        this.$refs['dataForm'].validate((valid) => {
          if (valid) {
            this.$http({
              url: this.$http.adornUrl(`/help/user/${!this.dataForm.id ? 'save' : 'update'}`),
              method: 'post',
              data: this.$http.adornData({
                'id': this.dataForm.id || undefined,
                'name': this.dataForm.name,
                'phone': this.dataForm.phone,
                'dphone': this.dataForm.dphone,
                'avatarurl': this.dataForm.avatarurl,
                'showFee': this.dataForm.showFee,
                'zfb': this.dataForm.zfb
              })
            }).then(({data}) => {
              if (data && data.code === 0) {
                this.$message({
                  message: '操作成功',
                  type: 'success',
                  duration: 1500,
                  onClose: () => {
                    this.visible = false
                    this.$emit('refreshDataList')
                  }
                })
              } else {
                this.$message.error(data.msg)
              }
            })
          }
        })
      }
    }
  }
</script>
